package com.lwj.controller.administration;

import com.lwj.annotation.RequirePermission;
import com.lwj.dto.administration.UserDto;
import com.lwj.dto.administration.UserListDto;
import com.lwj.entity.RestEntity;
import com.lwj.service.administration.UserService;
import com.lwj.vo.PageBean;
import com.lwj.vo.administration.DepartmentVo;
import com.lwj.vo.administration.UserVo;
import com.lwj.vo.configuration.RoleInfo;
import com.lwj.vo.configuration.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags ="用户接口")
@RestController()
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public RestEntity<UserInfo> login(@RequestBody UserDto userDto){
        return RestEntity.success(userService.login(userDto.getEmpId(),userDto.getPassword()));
    }
    @ApiOperation(value = "退出")
    @PostMapping("/out")
    public RestEntity out(){
        userService.out();
        return RestEntity.success();
    }
    @ApiOperation(value = "新增用户")
    @PostMapping("/save")
    @RequirePermission("administration.user.save")
    public RestEntity insertUser(@RequestBody  @Valid UserDto dto){
        return RestEntity.success(userService.save(dto));
    }
    @ApiOperation(value = "查询用户")
    @PostMapping("/selectUser")
    @RequirePermission("administration.user.select")
    public RestEntity selectUser(@RequestBody UserDto dto){
        return RestEntity.success(userService.getUserById(dto));
    }

    @ApiOperation(value = "禁用用户")
    @GetMapping("/changeState")
    @RequirePermission("administration.user.save")
    public RestEntity disabled(@RequestParam("enableState") Integer enableState,@RequestParam("empId") String empId){
        return RestEntity.success(userService.changeState(empId,enableState));
    }

    @ApiOperation(value = "删除用户")
    @GetMapping("/delete")
    @RequirePermission("administration.user.save")
    public RestEntity delete(@RequestParam("empId") String empId){
        return RestEntity.success(userService.delete(empId));
    }

    @ApiOperation(value = "查询部门用户")
    @PostMapping("/selectUserByDepartmentId")
    @RequirePermission("administration.user.select")
    public RestEntity<PageBean<UserVo>> selectUserByDepartmentId(@RequestBody UserListDto dto){
        return RestEntity.success(userService.selectUserByDepartmentId(dto));
    }

    @ApiOperation(value = "查询用户信息")
    @GetMapping("/selectUserRoles")
    public RestEntity< List<RoleInfo> > selectUserRoles(){
        return RestEntity.success(userService.selectUserRoles());
    }
}
